package com.example.android_webservice_demo;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.example.webservice.WSUtil;

import android.net.LocalSocketAddress.Namespace;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final String TAG = "android_webservice_demo";
	// 命名空间
	private static final String ServiceNameSpace = "http://tempuri.org/";
	// webServer地址
	private static final String webServiceUrl = "http://192.168.0.10/MyService/DateTimeService.asmx";
	// webServer调用方法名
	private static final String GetNowTime = "GetNowTime";

	Button getCurrentDateTimeButton;
	TextView tvCurrentDateTimeTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tvCurrentDateTimeTextView = (TextView) findViewById(R.id.tvCurrentDateTime);
		getCurrentDateTimeButton = (Button) findViewById(R.id.GetCurrentDateTime);
		getCurrentDateTimeButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				new Thread(runnable).start();
			}
		});

	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			Bundle data = msg.getData();
			String val = data.getString("value");
			tvCurrentDateTimeTextView.setText(val);
			Log.i(TAG, val);

		};

	};

	Runnable runnable = new Runnable() {

		@Override
		public void run() {

			try {
				Object object = WSUtil.getObjectByCallingWS(ServiceNameSpace,
						GetNowTime, null, webServiceUrl);

				Message msg = new Message();
				Bundle data = new Bundle();
				
				data.putString("value", object.toString());
				msg.setData(data);
				handler.sendMessage(msg);
				Log.i(TAG, object.toString());

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			/*
			 * SoapObject request = new SoapObject(ServiceNameSpace,
			 * GetNowTime); SoapSerializationEnvelope envelope = new
			 * SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.dotNet =
			 * true; envelope.setOutputSoapObject(request); HttpTransportSE
			 * transport = new HttpTransportSE(webServiceUrl); transport.debug =
			 * true;
			 * 
			 * try { transport.call(ServiceNameSpace + GetNowTime, envelope); if
			 * (envelope.getResponse() != null) { Message msg = new Message();
			 * Bundle data = new Bundle(); data.putString("value",
			 * envelope.bodyIn.toString()); msg.setData(data);
			 * handler.sendMessage(msg); } } catch (IOException e) { // TODO
			 * Auto-generated catch block e.printStackTrace(); } catch
			 * (XmlPullParserException e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); }
			 */
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
